<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$id=$_RQ['id'];
	
	$ad=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'isdefault'=>1));
	if(!$ad){
		exi('请先完善默认收货地址','error','core/member.home/');
	}
	$return['id']=$id;
	$good=pdo_get('jy_supplier_goods',array('id'=>$id),array('image','title','price','minbuy','id','unit'));
	$return['goods']=$good;
	$title='供货商商品:'.$good['title'];
	$return['title']=$title;
	$return['money']=$good['price'];
	$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'isdefault'=>1));
	$pcd=json_decode($address['pcd'],true);
	$tid='gh'.time().rand(10000,99999);
	$address['address']=$pcd['province'].$pcd['city'].$pcd['district'].$address['address'];
	$return['post']['address']=$address;
	$return['mixbuy']=$good['minbuy']?$good['minbuy']:0;
	$return['address']=$address;
	 if($_RQ['postdata']){
		$postdata=get_postdata();
		$id=$postdata['id'];
		$good=pdo_get('jy_supplier_goods',array('id'=>$id),array('image','title','price','minbuy','id','sid','totalcnf'));
		
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'isdefault'=>1));
		//$info=pdo_get('jy_supplier_goods',array('id'=>$id));
		$TIME=time();
		$tid='gh'.$TIME.rand(10000,99999);
		$money=$good['price'] * intval($postdata['num']);
		$data=array('ordersn'=>$tid,'goodsid'=>$id,'num'=> intval($postdata['num']),'uniacid'=>$_SESSION['uniacid'],'price'=>$money,'sid'=>$good['sid'],'createtime'=>time(),'status'=>0,'mid'=>$_SESSION['uid'],'addressid'=>$address['id']);
		pdo_insert('jy_supplier_order',$data); 
		$oid=pdo_insertid();
		if($good['totalcnf']==0){
			$data1=array('salenum +='=>$postdata['num'],'total -='=>$postdata['num']);
			pdo_update('jy_supplier_goods',$data1,array('id'=>$id));
			
		}
		if($good['totalcnf']==2){
			$data1=array('salenum +='=>$postdata['num']);
			pdo_update('jy_supplier_goods',$data1,array('id'=>$id));
		}
		
		
		$title=$good['title'];
		
		$params=array('title'=>$title,'fee'=>$money,'id'=>$oid,'plugin'=>'jy_supplier');	
		$params['back_url']='jy_supplier/create/payresult';
		
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	if(!$id){
		exi('参数错误','error');
	}
	
	$return['info']=$info;
	exi($return);	
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	
	$order=pdo_get('jy_supplier_order',array('id'=>$payresult['id']));
	
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_supplier_order',array('status'=>1,'ispay'=>1,'paytime'=>time(),'paytype'=>$payresult['pay_type']),array('id'=>$order['id']));
	$type=pdo_getcolumn('jy_supplier_goods',array('id'=>$order['gid']),'totalcnf');
	if($type==1){
		$data=array('salenum +='=>$order['num'],'total -='=>$order['num']);
		pdo_update('jy_supplier_goods',$data,array('id'=>$order['gid']));
	}
	
	exi('支付成功','','jy_supplier/allgoods/');
}
